package co.fitstart.weapp.api.dao;

import java.util.Date;
import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;

import co.fitstart.entity.coupon.Coupon;
import co.fitstart.entity.coupon.CouponStatus;
import co.fitstart.entity.order.Order;

@Dao("hibernate")
public interface CouponDao {
    
    /**
     * 
     * @param userId
     * @return
     */
    @Query("FROM Coupon WHERE userId = :userId AND credit = false ORDER BY status ASC")
    public List<Coupon> list(@Variable("userId") Long userId);
    
    /**
     * 
     * @param order
     * @return
     */
    @Query("FROM Coupon WHERE order = :order")
    public List<Coupon> list(@Variable("order") Order order);

    /**
     * 
     * @param mobile
     * @param openid
     * @return
     */
    @Query("FROM Coupon coupon WHERE (coupon.identification = :mobile OR coupon.openid = :openid) AND coupon.userId IS NULL")
    public List<Coupon> list(@Variable("mobile") String mobile, @Variable("openid") String openid);

    /**
     * 
     * @param userId
     * @param couponStatuss
     * @param now
     * @return
     */
    @Query("FROM Coupon WHERE userId = :userId AND effectiveStartDate <= :now AND effectiveEndDate >= :now AND status = :claimed")
    public List<Coupon> listAvailable(@Variable("userId") Long userId, @Variable("claimed") CouponStatus claimed, @Variable("now") Date now);

    /**
     * 
     * @param userId
     * @param claimed
     * @param couponStatuss
     * @param now
     * @return
     */
    @Query("FROM Coupon WHERE originalUserId = :userId OR (userId = :userId AND ((effectiveEndDate < :now AND status = :claimed) OR status IN (:couponStatuss)))")
    public List<Coupon> listUnavailable(@Variable("userId") Long userId, @Variable("claimed") CouponStatus claimed, @Variable("couponStatuss") List<CouponStatus> couponStatuss, @Variable("now") Date now);
}
